package easy;


/**
 * @author admin
 * 704. 二分查找
 * 解题思路：典中典的二分查找算法
 */
public class LeeCode704 {

    public int search(int[] nums, int target) {

        int left = 0;
        int right = nums.length - 1;
        while (left <= right){
            int mid = (left + right)/2;
            int res = nums[mid];
            if (target == res){
                return mid;
            }else if(target < res){
                right = mid - 1;
            }else {
                left = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(new LeeCode704().search(new int[]{-1, 0, 3, 5, 9, 12}, 9));
    }
}
